package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.RcglCommunityServicesCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.RcglCommunityServicesResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.RcglCommunityServicesUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.RcglCommunityServicesEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface RcglCommunityServicesConvert {

    RcglCommunityServicesConvert INSTANCE = Mappers.getMapper(RcglCommunityServicesConvert.class);

    RcglCommunityServicesEntity createTo(RcglCommunityServicesCreateDTO s);

    RcglCommunityServicesEntity updateTo(RcglCommunityServicesUpdateDTO s);

    RcglCommunityServicesResultDTO convertTo(RcglCommunityServicesEntity s);

    default RcglCommunityServicesEntity create(RcglCommunityServicesCreateDTO s, Long tenantId) {
        RcglCommunityServicesEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default RcglCommunityServicesEntity update(RcglCommunityServicesUpdateDTO s) {
        RcglCommunityServicesEntity entity = updateTo(s);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default RcglCommunityServicesResultDTO convert(RcglCommunityServicesEntity s) {
        RcglCommunityServicesResultDTO result = convertTo(s);
        renderAreaList(result);
        return result;
    }

    default void additionalArea(RcglCommunityServicesEntity s, List<String> areaList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(areaList)) {
            return;
        }
        s.setSqfwszs(areaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        s.setSqfwszds(areaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (areaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            s.setSqfwszxq(areaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            s.setSqfwszxq(null);
        }

        if (areaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            s.setSqfwszd(areaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            s.setSqfwszd(null);
        }
    }

    default void renderAreaList(RcglCommunityServicesResultDTO s) {
        if (Objects.isNull(s)) {
            return;
        }

        List<String> residenceAreaList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(s.getSqfwszs()), () -> residenceAreaList.add(s.getSqfwszs()));
        Actions.apply(StringUtils.isNotEmpty(s.getSqfwszds()), () -> residenceAreaList.add(s.getSqfwszds()));
        Actions.apply(StringUtils.isNotEmpty(s.getSqfwszxq()), () -> residenceAreaList.add(s.getSqfwszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getSqfwszd()), () -> residenceAreaList.add(s.getSqfwszd()));

        s.setAreaList(residenceAreaList);
    }
}
